package whitebox;
import static org.junit.Assert.assertEquals;

import java.lang.reflect.Field;

import javax.swing.JTextField;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import blackjack.MyMath;
import blackjack.apple_mod;

/* CPU METHOD SPECIFICATION
* 1. 	The method simulate the cpu game.
* 2.	The cpu hits until cpu_score < player_score.
* 3.	The cpu hits until cpu_score < 17.
* 4. 	If cpu is the winner it show a label "Lose!" otherwise it show a label "Win!".
* 5.	In case of tie, the game ends and it show a label "Push!".
* 
*The CFG generated with a tool of eclipse show the McGaber result is 3.
*Then we need 3 test to cover all code and test it.
*
*TESTS SUMMARY
*1:	player = 20 && rand_list = {8,1,2}			CPU win with 21
*2:	player = 21 && rand_list = {4,8,6,5}		CPU lose with busted (>21)
*3:	player = 19 && rand_list = {8,1};			CPU push with 19
*4:	player = 18 && rand_list = {8,6,5}			CPU win with <21
*/


public class TestCpu {
	private apple_mod test;
	
	private JTextField field;
	private String fieldString;
	
	private Field fieldStringField,playerField;
	private Class testClass;
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
		test = new apple_mod();
		
		testClass = test.getClass();
		
		fieldStringField = testClass.getDeclaredField("field");
		fieldStringField.setAccessible(true);
		
		playerField = testClass.getDeclaredField("player");
		playerField.setAccessible(true);
		
		
		field = (JTextField)fieldStringField.get(test);
	}

	@After
	public void tearDown() throws Exception {
	}

	/*
	 * player = 20 && rand_list = {8,1,2}
	 * CPU Vince facendo 21
	 */
	@Test
	public void testCpuWB1() throws IllegalArgumentException, IllegalAccessException {
		playerField.setInt(test, 20);
		
		double[] rand_list = {8,1,2};
		MyMath.set_randList(rand_list);
		
		test.cpu();
		fieldString = field.getText();
		
		assertEquals("Lose!", fieldString);

	}
	
	/*
	 * player = 21 && rand_list = {4,8,6,5}
	 * CPU Perde andando oltre 21
	 */
	@Test
	public void testCpuWB2() throws IllegalArgumentException, IllegalAccessException {
		playerField.setInt(test, 21);
		
		double[] rand_list = {4,8,6,5};
		MyMath.set_randList(rand_list);
		
		test.cpu();
		fieldString = field.getText();
		
		assertEquals("Win!", fieldString);
	}
	
	
	/*
	 * player = 19 && rand_list = {8,1};
	 * CPU Pareggia
	 */
	@Test
	public void testCpuWB3() throws IllegalArgumentException, IllegalAccessException {
		playerField.setInt(test, 19);
		
		double[] rand_list = {8,1};
		MyMath.set_randList(rand_list);
		
		test.cpu();
		fieldString = field.getText();
		
		assertEquals("Push!", fieldString);
	}
	
	

	
}
